package com.wzdigit.wms.wms.service.basic;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wzdigit.framework.Shift;
import com.wzdigit.wms.basic.domain.basic.ZoneGroupAttr;
import com.wzdigit.wms.basic.util.HttpSessionUtil;
import com.wzdigit.wms.basic.client.Pager;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.basic.CreateUpdateListZoneGroupAttrRequest;
import com.wzdigit.wms.basic.client.request.basic.CreateUpdateListZoneGroupAttrRequest.CreateUpdateZoneGroupAttrRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.basic.ZoneGroupAttrResponse;
import com.wzdigit.wms.common.entity.StatusCode;
import com.wzdigit.wms.basic.mapper.core.basic.ZoneGroupAttrMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 区域-属性 服务类
 * @author yangdongjie
 * @ZoneGroupAttrService.java
 * @2021年1月4日上午11:16:31
 */
@Service
public class ZoneGroupAttrService {

    @Autowired
    private ZoneGroupAttrMapper zoneGroupAttrMapper;

    public <T> PagerInfo<List<ZoneGroupAttrResponse>> searchZoneGroupAttr(
    		Integer zoneGroupId, Pager<T> pager) {
    	Page<T> page = PageHelper.offsetPage(pager.getSkipCount(), pager.getMaxResultCount());
        PagerInfo<List<ZoneGroupAttrResponse>> pagerInfo = new PagerInfo<List<ZoneGroupAttrResponse>>();
        List<ZoneGroupAttrResponse> list = zoneGroupAttrMapper.selectAllByZoneGroupId(zoneGroupId);
        pagerInfo.setItems(list);
        pagerInfo.setTotalCount(page.getTotal());
		return pagerInfo;
    }
    
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public <T> Result<T> createUpdateListZoneGroupAttr(CreateUpdateListZoneGroupAttrRequest dto,
    		HttpServletRequest servletRequest) {
    	String loginName = HttpSessionUtil.getLoginName(servletRequest);
    	Date date = new Date();
    	
    	// 记录不删除的属性ID
    	List<Integer> notDelIds = new ArrayList<Integer>();
    	
    	List<CreateUpdateZoneGroupAttrRequest> zoneGroupAttrs = dto.getZoneGroupAttrs();
    	
    	// 去重后的大小
    	Long count = zoneGroupAttrs.stream().map(one -> one.getAttr()).distinct().count();
    	if(count.intValue() != zoneGroupAttrs.size()) {
    		Shift.fatal(StatusCode.ATTR_DATA_EXIST);
    	}
    	
    	ZoneGroupAttr zoneGroupAttr = new ZoneGroupAttr();
    	zoneGroupAttr.setZoneGroupId(dto.getZoneGroupId());
		for (CreateUpdateZoneGroupAttrRequest one : zoneGroupAttrs) {
			zoneGroupAttr.setAttr(one.getAttr());
			zoneGroupAttr.setValue(one.getValue());
			zoneGroupAttr.setComments(one.getComments());
			zoneGroupAttr.setIsEnabled(one.getIsEnabled());
			if (one.getZoneGroupAttrId() == null) {
				zoneGroupAttr.setCreateUser(loginName);
				zoneGroupAttr.setCreateTime(date);
				zoneGroupAttrMapper.insert(zoneGroupAttr);
			} else {
				zoneGroupAttr.setZoneGroupAttrId(one.getZoneGroupAttrId());
				zoneGroupAttr.setModifyUser(loginName);
				zoneGroupAttr.setModifyTime(date);
				zoneGroupAttrMapper.updateById(zoneGroupAttr);
			}
			notDelIds.add(zoneGroupAttr.getZoneGroupAttrId());
		}
		
		// 删除属性
		if (notDelIds.size() > 0) {
			zoneGroupAttrMapper.deleteByZoneGroupIdAndNotId(dto.getZoneGroupId(), notDelIds);
		} else {
			zoneGroupAttrMapper.deleteByZoneGroupId(dto.getZoneGroupId());
		}
    	return new Result<>();
    }
    
    /*@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public ZoneGroupAttrResponse createZoneGroupAttr(CreateZoneGroupAttrRequest dto,
    		HttpServletRequest servletRequest) {
    	String loginName = HttpSessionUtil.getLoginName(servletRequest);
    	ZoneGroupAttr zoneGroupAttr = zoneGroupAttrMapper.selectOneByAttrAndZoneGroupId(dto.getAttr(), dto.getZoneGroupId());
    	if(zoneGroupAttr != null) {
    		// 属性已存在
    		Shift.fatal(StatusCode.ATTR_DATA_EXIST);
    	}
    	zoneGroupAttr = new ZoneGroupAttr();
    	BeanUtil.copyNonNull(dto, zoneGroupAttr);
    	zoneGroupAttr.setCreateUser(loginName);
    	zoneGroupAttr.setCreateTime(new Date());
    	zoneGroupAttrMapper.insert(zoneGroupAttr);
    	
    	ZoneGroupAttrResponse response = new ZoneGroupAttrResponse();
    	BeanUtil.copyNonNull(zoneGroupAttr, response);
		return response;
    }
    
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public ZoneGroupAttrResponse updateZoneGroupAttr(UpdateZoneGroupAttrRequest dto,
    		HttpServletRequest servletRequest) {
    	String loginName = HttpSessionUtil.getLoginName(servletRequest);
    	ZoneGroupAttr zoneGroupAttr = zoneGroupAttrMapper.selectOneByAttrAndZoneGroupId(dto.getAttr(), dto.getZoneGroupId());
    	if(zoneGroupAttr != null) {
    		if (dto.getZoneGroupAttrId().intValue() != zoneGroupAttr.getZoneGroupAttrId().intValue()) {
    			// 属性已存在
    			Shift.fatal(StatusCode.ATTR_DATA_EXIST);
			}
    	}
    	zoneGroupAttr = zoneGroupAttrMapper.selectByPrimaryKey(dto.getZoneGroupAttrId());
    	BeanUtil.copy(dto, zoneGroupAttr);
    	zoneGroupAttr.setModifyUser(loginName);
    	zoneGroupAttr.setModifyTime(new Date());
    	zoneGroupAttrMapper.updateById(zoneGroupAttr);
    	
    	ZoneGroupAttrResponse response = new ZoneGroupAttrResponse();
    	BeanUtil.copy(zoneGroupAttr, response);
		return response;
    }
    
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public <T> Result<T> deleteZoneGroupAttr(Integer whsAttrId) {
    	zoneGroupAttrMapper.deleteByPrimaryKey(whsAttrId);
		return new Result<>();
    }*/
}
